package com.example.demo.common;

import javax.sql.DataSource;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-09-17
 * Time: 18:11
 */
public class MyThreadPool {
    private static final int CORE_POOL_SIZE = 2; // 核心线程数
    private static final int MAXIMUM_POOL_SIZE = 10; // 最大线程数
    private static final long KEEP_ALIVE_TIME = 60L; // 线程空闲时间（单位：秒）
    private static final int CAPACITY = 50; // 有界队列最多可以容纳的任务

    // 线程池（单例模式）
    private static volatile ThreadPoolExecutor threadPool = null;
    public static ThreadPoolExecutor getThreadPool() {
        if (threadPool == null) {
            synchronized (MyThreadPool.class) {
                if (threadPool == null) {
                    threadPool = new ThreadPoolExecutor(
                            CORE_POOL_SIZE,
                            MAXIMUM_POOL_SIZE,
                            KEEP_ALIVE_TIME,
                            TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(CAPACITY)
                    );
                }
            }
        }
        return threadPool;
    }
}
